/*
 * @Author: zxh
 * @Date: 2024-07-29 11:38:32
 * @LastEditors: zxh
 * @LastEditTime: 2024-07-29 14:08:23
 * @Description: 封装分类数据业务相关代码
 */
import { ref, onMounted } from 'vue'
import { onBeforeRouteUpdate, useRoute } from 'vue-router'
import { getTopCategoryAPI } from '@/apis/category'
export function useCategoty() {
  const categoryData: any = ref<any>({})
  // 如何在setup中获取路由参数 useRoute() -> route 等价于this.$route
  const route = useRoute()
  const getCategory = async (id: string | string[]) => {
    const res = await getTopCategoryAPI(id)
    categoryData.value = res.result
  }
  // 监听路由参数变化的时候 可以把分类数据接口重新发送
  onBeforeRouteUpdate((to) => {
    // 使用最新的路由参数请求最新的分类数据
    getCategory(to.params.id)
  })
  onMounted(() => {
    getCategory(route.params.id)
  })
  return {
    categoryData
  }
}
